﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Entities;
using SD.DAL;
using SD.IDAL;
using SD.IBUS;
using System.Data;
using System.Data.SqlClient;

namespace SD.BUS
{
    public class DepartmentBIZ : BaseBIZ, IDepartmentBIZ
    {
        #region IDepartmentDAO Members
        /// <summary>
        /// Create DepartmentDAO class
        /// </summary>
        IDepartmentDAO departmentDAO = new DepartmentDAO();
        /// <summary>
        /// Get List Department by Filter
        /// </summary>
        /// <param name="filterExpression"></param>
        /// <param name="includeInactive"></param>
        /// <param name="directorateID"></param>
        /// <returns></returns>
        public DataTable GetByFilter(string filterExpression, bool includeInactive, string directorateID)
        {
            DataTable dtbResult = new DataTable();
            //Create DataColumn
            dtbResult.Columns.Add("DepartmentID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Department> lstDepartment = departmentDAO.GetByFilter(filterExpression, includeInactive, directorateID);
                if (lstDepartment.Count > 0)
                {
                    foreach (Department depart in lstDepartment)
                    {
                        dtbResult.Rows.Add(depart.DepartmentId, depart.Name,
                                            depart.Address != null ? depart.Address.Line1 : "N/A",
                                            depart.Address != null ? depart.Address.PostCode : "N/A",
                                            depart.Contact != null ? depart.Contact.FirstName : "N/A",
                                            depart.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Get All List Department
        /// </summary>
        /// <param name="includeInactive"></param>
        /// <param name="directorateID"></param>
        /// <returns></returns>
        public DataTable GetAll(bool includeInactive, string directorateID)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("DepartmentID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Department> lstDepart = departmentDAO.GetAll(includeInactive, directorateID);
                if (lstDepart.Count > 0)
                {
                    foreach (Department depart in lstDepart)
                    {
                        dtbResult.Rows.Add(depart.DepartmentId, depart.Name,
                                            depart.Address != null ? depart.Address.Line1 : "N/A",
                                            depart.Address != null ? depart.Address.PostCode : "N/A",
                                            depart.Contact != null ? depart.Contact.FirstName : "N/A",
                                            depart.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Get Department by Name
        /// </summary>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public List<Department> GetDirByName(string dirName)
        {
            List<Department> result = new List<Department>();

            try
            {
                result = departmentDAO.GetDirByName(dirName);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Get a Department by ID
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public Department GetById(string departmentId)
        {
            Department result = null;

            try
            {
                result = departmentDAO.GetById(departmentId);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Insert a Department
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public void Insert(Department department)
        {
            try
            {
                department.DepartmentId = departmentDAO.Insert(department);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Update a Department
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public void Update(Department department)
        {
            try
            {
                departmentDAO.Update(department);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Set Active a Department
        /// </summary>
        /// <param name="departId"></param>
        /// <param name="status"></param>
        public void SetActive(string departId, bool status)
        {
            try
            {
                departmentDAO.SetActive(departId, status);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }
        public bool Delete(string orgId)
        {
            return false;
        }

        #endregion
    }
}
